package com.xbongbong.saas.service.impl;

import com.xbongbong.paas.elasticsearch.model.PaasFormDataEsModel;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.purchaseproduct.pojo.dto.PurchaseProductDeleteBatchDTO;
import com.xbongbong.pro.purchaseproduct.pojo.dto.PurchaseProductListDTO;
import com.xbongbong.pro.purchaseproduct.pojo.vo.PurchaseProductDeleteBatchVO;
import com.xbongbong.pro.purchaseproduct.pojo.vo.PurchaseProductListVO;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.domain.entity.PurchaseProductEntity;
import com.xbongbong.saas.domain.entity.ext.PurchaseEntityExt;
import com.xbongbong.saas.model.PurchaseProductModel;
import com.xbongbong.saas.service.PurchaseProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 *
 * @author Jian.Wang
 * @version v1.0
 * @date 2019-02-21 16:01
 * @since v1.0
 **/
@Service("PurchaseProductService")
public class PurchaseProductServiceImpl implements PurchaseProductService{

    private static final Logger LOG = LoggerFactory.getLogger(PurchaseProductServiceImpl.class);
    @Resource
    private PurchaseProductModel purchaseProductModel;
    @Resource
    private PaasFormDataEsModel paasFormDataEsModel;
    /**
     * 插入数据
     *
     * @param purchaseProductEntity 采购合同产品实体数据
     * @return 是否成功
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     **/
    @Override
    public Integer insert(PurchaseProductEntity purchaseProductEntity) {
        return purchaseProductModel.insert(purchaseProductEntity);
    }

    /**
     * 更新数据
     *
     * @param purchaseProductEntity 采购合同产品实体数据
     * @return 是否成功
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     **/
    @Override
    public Integer update(PurchaseProductEntity purchaseProductEntity) {
        return purchaseProductModel.update(purchaseProductEntity);
    }

    /**
     * 保存数据
     *
     * @param purchaseProductEntity 保存实体
     * @return java.lang.Integer
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public Integer save(PurchaseProductEntity purchaseProductEntity) {
        if (purchaseProductEntity.getId() == null || purchaseProductEntity.getId().equals(0L)) {
            return purchaseProductModel.insert(purchaseProductEntity);
        }
        return purchaseProductModel.update(purchaseProductEntity);
    }

    /**
     * 根据主键删除记录
     *
     * @param id     主键
     * @param corpid 公司id
     * @return java.lang.Integer
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public Integer deleteByKey(Long id, String corpid) {
        return purchaseProductModel.deleteByKey(id,corpid);
    }

    /**
     * 删除采购合同下的所有产品
     *
     * @param purchaseSheetId id集合
     * @param corpid          公司id
     * @return java.lang.Integer
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public Integer deleteByPurchaseId(Long purchaseSheetId, String corpid) {
        return purchaseProductModel.deleteByPurchaseId(purchaseSheetId,corpid);
    }

    @Override
    public PurchaseProductDeleteBatchVO deleteByPurchaseIdIn(PurchaseProductDeleteBatchDTO purchaseProductDeleteBatchDTO) throws XbbException {
        PurchaseProductDeleteBatchVO purchaseProductDeleteBatchVO = new PurchaseProductDeleteBatchVO();
        try {
            List<Long> purchaseSheetIdIn = purchaseProductDeleteBatchDTO.getPurchaseIdIn();
            String corpid = purchaseProductDeleteBatchDTO.getCorpid();
            purchaseProductModel.deleteByPurchaseIdIn(purchaseSheetIdIn,corpid);
        } catch (Exception e) {
            LOG.error("PurchaseProductServiceImpl.deleteByPurchaseIdIn 执行出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return purchaseProductDeleteBatchVO;
    }

    /**
     * 获取一条采购合同产品数据
     *
     * @param id     采购合同产品ID
     * @param corpid 公司id
     * @return PurchaseProductEntity 一条数据实体
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public PurchaseProductEntity getByKey(Long id, String corpid) {
        return purchaseProductModel.getByKey(id,corpid);
    }

    /**
     * 多参数查询多条数据
     *
     * @param param 参数集合
     * @return List<PurchaseProductEntity> 数据集合
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public List<PurchaseProductEntity> findEntitys(Map<String, Object> param) {
        return purchaseProductModel.findEntitys(param);
    }

    /**
     * 获取记录数
     *
     * @param param 参数集合
     * @return List<Long> Id集合
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return purchaseProductModel.getEntitysCount(param);
    }

    /**
     * 根据参数集合获取所有的采购合同产品
     *
     * @param param 参数集合
     * @return List<Long> Id集合
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public List<PurchaseProductEntity> getAllByPurchaseId(Map<String, Object> param) {
        return purchaseProductModel.getAllByPurchaseId(param);
    }

    /**
     * 关联查询
     *
     * @param param 参数集合
     * @return List<Long> Id集合
     * @author Jian.Wang
     * @date 2019/2/21 15:18
     */
    @Override
    public List<PurchaseProductEntity> findEntitysJoinProduct(Map<String, Object> param) {
        return purchaseProductModel.findEntitysJoinProduct(param);
    }

    /**
     * 导入采购合同时保存关联产品
     *
     * @param purchase 采购合同
     * @author GuoJun.HU
     * @since 3.26
     * 创建时间 2018/7/24 14:11
     */
    @Override
    public void setPurchaseProduct(PurchaseEntityExt purchase) {

    }

    /**
     * 获取采购合同关联产品
     *
     * @param corpid      公司id
     * @param purchaseId  采购合同id
     * @param containsDel 是否包含已被删除的产品
     * @return 采购合同产品列表
     * @author Jian.Wang
     * 创建时间：2018-02-21 16:16
     */
    @Override
    public List<PurchaseProductEntity> getProductsByPurchaseId(String corpid, Long purchaseId, boolean containsDel) {
        Map<String, Object> param = new HashMap<>(16);
        param.put(ParameterConstant.CORPID, corpid);
        param.put(ParameterConstant.PURCHASE_SHEET_ID, purchaseId);
        if (!containsDel) {
            param.put(ParameterConstant.DEL, 0);
        }
        return findEntitys(param);
    }

    /**
     * 采购合同和关联产品的对应关系
     *
     * @param corpid       公司id
     * @param purchaseList 采购合同id
     * @param containsDel  是否包含已被删除的产品
     * @author GuoJun.HU
     * @since 3.26
     * 创建时间 2018/8/3 18:13
     */
    @Override
    public Map<Long, List<PurchaseProductEntity>> getProductsByPurchaseList(String corpid, Collection<PurchaseEntityExt> purchaseList, boolean containsDel) {
        List<Long> purchaseIdIn = new ArrayList<>();
        for (PurchaseEntityExt purchaseEntityExt : purchaseList) {
            purchaseIdIn.add(purchaseEntityExt.getId());
        }
        if (purchaseIdIn.isEmpty()) {
            return new HashMap<>(16);
        }

        Map<String, Object> param = new HashMap<>(16);
        param.put(ParameterConstant.CORPID, corpid);
        param.put(ParameterConstant.PURCHASE_ID_IN, purchaseIdIn);
        if (!containsDel) {
            param.put(ParameterConstant.DEL, 0);
        }
        List<PurchaseProductEntity> allPurchaseProductList = findEntitys(param);
        return wrapPurchaseListToMap(allPurchaseProductList);
    }

    /**
     * 采购合同和关联产品的对应关系
     *
     * @param allPurchaseProductList 采购合同关联产品列表
     * @author GuoJun.HU
     * @since 3.26
     * 创建时间 2018/8/3 19:13
     */
    @Override
    public Map<Long, List<PurchaseProductEntity>> wrapPurchaseListToMap(List<PurchaseProductEntity> allPurchaseProductList) {
        Map<Long, List<PurchaseProductEntity>> purchaseProductMap = new HashMap<>(16);

        for (PurchaseProductEntity purchaseProductEntity : allPurchaseProductList) {
            Long purchaseId = purchaseProductEntity.getPurchaseSheetId();

            List<PurchaseProductEntity> purchaseProductList;
            if (purchaseProductMap.containsKey(purchaseId)) {
                purchaseProductList = purchaseProductMap.get(purchaseId);
            } else {
                purchaseProductList = new ArrayList<>();
            }
            purchaseProductList.add(purchaseProductEntity);
            purchaseProductMap.put(purchaseId, purchaseProductList);
        }

        return purchaseProductMap;
    }

    /**
     * 通过产品名称或编号查询对应的采购合同id
     *
     * @param corpid       公司id
     * @param nameOrNoLike 产品名称或编号
     * @author lcx
     * 创建时间 2018/7/18 10:08
     */
    @Override
    public List<Long> findPurchaseIds(String corpid, String nameOrNoLike) {
        Map<String, Object> param = new HashMap<>(16);
        param.put(ParameterConstant.CORPID, corpid);
        param.put(ParameterConstant.NAME_OR_NO_LIKE, nameOrNoLike);
        param.put(ParameterConstant.DEL, 0);
        return purchaseProductModel.findPurchaseIds(param);
    }

    @Override
    public PurchaseProductListVO findPurchaseProduct(PurchaseProductListDTO purchaseProductListDTO) throws XbbException {
        PurchaseProductListVO purchaseProductListVO = new PurchaseProductListVO();
        try {
            Map<String, Object> param = BeanUtil.convertBean2Map(purchaseProductListDTO, true);
            if (Objects.isNull(purchaseProductListDTO.getProductIdList()) && Objects.isNull(purchaseProductListDTO.getPurchaseSheetIdIn())) {
                purchaseProductListVO.setPurchaseProductList(new ArrayList<>());
                return purchaseProductListVO;
            }
            param.put("productIdIn", purchaseProductListDTO.getProductIdList());
            List<PurchaseProductEntity> purchaseProductList = purchaseProductModel.findEntitys(param);
            if(Objects.isNull(purchaseProductList)) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
            }
            purchaseProductListVO.setPurchaseProductList(purchaseProductList);
        } catch (Exception e) {
            LOG.error("PurchaseProductServiceImpl.findPurchaseProduct 执行出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
        }
        return purchaseProductListVO;
    }

    /**
     * 多参数查询多条数据
     *
     * @param
     * @return List<PurchaseProductEntity> 数据集合
     * @author jiping.yang
     * @date 2021/09/09 15:18
     */
    public List<PurchaseProductEntity> findProductEntitys(FormDataListDTO formDataListDTO,Map<String, Object> param){
        return purchaseProductModel.findProductEntitys(param);
    }

    /**
     * 获取记录数
     *
     * @param param 参数集合
     * @return List<Long> Id集合
     * @author jiping.yang
     * @date 2021/9/17 15:18
     */
    @Override
    public Integer getPurchaseProductCount(Map<String, Object> param) {
        return purchaseProductModel.getPurchaseProductCount(param);
    }

    /**
     * 根据合同id和corpid查询数据
     *
     * @param
     * @return List<PurchaseProductEntity> 数据集合
     * @author jiping.yang
     * @date 2021/10/09 15:18
     */
    public List<PurchaseProductEntity> getContractPurchase(String corpid , Long dataId){
        return purchaseProductModel.getContractPurchase(corpid , dataId);
    }
}
